<template>
  <div>
    <ZgDataImport
      ref="import"
      :showBtn="false"
      fileName="test.xlsx"
      url="/zg-system/dic/import"
      @ok="onImportOk">
    </ZgDataImport>
    <ZgPage
      ref="page"
      title="字典管理"
      addBtnName="新增字典"
      :addOpt="{title:'新增字典'}"
      :updateOpt="{title:'编辑字典'}"
      :batchOpt="{title:'批量设置'}"
      :extraAddBtns="['批量导入','导出数据']"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
      @batch="onBatch"
      @extraAdd="onExtraAdd"
    >
      <template slot="columns">
        <a-table-column :width="140" dataIndex="name" title="字典名称"></a-table-column>
        <a-table-column :width="100" dataIndex="code" title="字典编码"></a-table-column>
        <a-table-column title="选项内容">
          <template slot-scope="text,record">
            <Children :code="record.code"></Children>
          </template>
        </a-table-column>
        <a-table-column :width="100" title="状态">
          <template slot-scope="text,record">
            {{ record.isUse?'启用':'禁用' }}
          </template>
        </a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
      <template slot="batch" slot-scope="scope">
        <Batch ref="batch" :ids="scope.ids"></Batch>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
import Batch from './Batch'
import Children from './Children'
export default {
  components: {
    Add, Update, Batch, Children
  },
  data () {
    return {
    }
  },
  methods: {
    onSearch (param) {
      this.$http.get('/zg-system/dic/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onDel (ids) {
      this.$http.post('/zg-system/dic/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/dic/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/dic/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    },
    onBatch () {
      const result = this.$refs.batch.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/dic/batch', result.data, () => {
        this.$refs.page.hideBatch()
        this.$refs.page.search()
      })
    },
    onExtraAdd (i) {
      if (i === 0) {
        this.$refs.import.open()
      } else if (i === 1) {
        this.$http.download('/zg-system/dic/export', {}, '字典数据.xlsx')
      }
    },
    onImportOk () {
      this.$refs.page.search()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
